package com.fourth.utils;

import com.fourth.conf.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @ClassName: BizAssertUtils
 * @Description: 自定义断言类
 *
 * @Date: 2023/2/27
 * @since: jdk 1.8
 **/
public class BizAssertUtils {
    private static final Logger logger = LoggerFactory.getLogger(BizAssertUtils.class);

    public BizAssertUtils() {
    }

    public static void isNull(Object object, String message, Object... params) {
        if (object != null) {
            logger.warn("应该为空: object={}, message={}, params={}", object, message, params);
            throw new BusinessException(message);
        }
    }
 
    public static void notNull(Object object, String message, Object... params) {
        if (object == null) {
            logger.warn("不应为空: object={}, message={}, params={}", null, message, params);
            throw new BusinessException(message);
        }
    }

    public static void isEmpty(Collection<?> collection, String message, Object... params) {
        if (collection != null && collection.size() != 0) {
            logger.warn("应该为空: collection={}, message={}, params={}", collection, message, params);
            throw new BusinessException(message);
        }
    }
 

    public static void notEmpty(Collection<?> collection, String message, Object... params) {
        if (collection == null || collection.size() == 0) {
            logger.warn("不应为空: collection={}, message={}, params={}", collection, message, params);
            throw new BusinessException(message);
        }
    }

 

    public static void notEmpty(Map<?, ?> map, String message, Object... params) {
        if (map == null || map.size() == 0) {
            logger.warn("不应为空: map={}, message={}, params={}", map, message, params);
            throw new BusinessException(message);
        }
    }

 

    public static void isBlank(String string, String message, Object... params) {
        if (StringUtils.isNotBlank(string)) {
            logger.warn("应该为空: string={}, message={}, params={}", string, message, params);
            throw new BusinessException(message);
        }
    }

 

    public static void notBlank(String string, String message, Object... params) {
        if (!StringUtils.isNotBlank(string)) {
            logger.warn("不应为空: string={}, message={}, params={}", string, message, params);
            throw new BusinessException(message);
        }
    }

 

    public static void isTrue(boolean flag, String message, Object... params) {
        if (!flag) {
            logger.warn("应该为TRUE: flag={}, message={},  params={}", flag, message, params);
            throw new BusinessException(message);
        }
    }
 

    public static void isFalse(boolean flag, String message, Object... params) {
        if (flag) {
            logger.warn("应该为FALSE: flag={}, message={}, params={}", flag, message, params);
            throw new BusinessException(message);
        }
    }

 

    public static <T extends Enum<T>> void legalEnum(Enum<T> actual, Enum<T> expected, String message, Object... params) {
        if (actual != expected) {
            logger.warn("状态错误：message={}, actual={}, expected={}, params={}", message, actual, expected, params);
            throw new BusinessException(message);
        }
    }
 

    public static <T extends Enum<T>> void legalEnums(String message, Enum<T> actual, Enum<T>... expected) {
        List<Enum<T>> enumList = Arrays.asList(expected);
        if (!enumList.contains(actual)) {
            logger.warn("状态错误：message={}, actual={}, expected={}", message, actual, expected);
            throw new BusinessException(message);
        }
    }
 

    public static <T extends Enum<T>> void legalEnums(Enum<T> actual, Enum<T>[] expected, String message, Object... params) {
        List<Enum<T>> enumList = Arrays.asList(expected);
        if (!enumList.contains(actual)) {
            logger.warn("状态错误：message={}, actual={}, expected={}, params={}", message, actual, expected, params);
            throw new BusinessException(message);
        }
    }
 

    public static void isEquals(String actual, String expected, String message, Object... params) {
        if (!Objects.equals(actual, expected)) {
            logger.warn("应该为相同：message={}, actual={}, expected={}, params={}", message, actual, expected, params);
            throw new BusinessException(message);
        }
    }
 

    public static void notEquals(String actual, String expected, String message, Object... params) {
        if (Objects.equals(actual, expected)) {
            logger.warn("应该不相同：message={}, actual={}, expected={}, params={}", message, actual, expected, params);
            throw new BusinessException(message);
        }
    }
 

    public static void isEquals(Long actual, Long expected, String message, Object... params) {
        if (!Objects.equals(actual, expected)) {
            logger.warn("应该为相同：message={}, actual={}, expected={}, params={}", message, actual, expected, params);
            throw new BusinessException(message);
        }
    }
 

    public static void notEquals(Long actual, Long expected, String message, Object... params) {
        if (Objects.equals(actual, expected)) {
            logger.warn("应该不相同：message={}, actual={}, expected={}, params={}", message, actual, expected, params);
            throw new BusinessException(message);
        }
    }
 

    public static void fail(String message, Object... params) {
        logger.warn("应该为失败: message={}, params={}", message, params);
        throw new BusinessException(message);
    }
 

    public static void isContains(String actual, Collection<String> expected, String message, Object... params) {
        if (!expected.contains(actual)) {
            logger.warn("匹配错误: actual={},expected={}, message={},params={}", actual, expected, message, params);
            throw new BusinessException(message);
        }
    }
 

    public static void notContains(String actual, Collection<String> expected, String message, Object... params) {
        if (expected.contains(actual)) {
            logger.warn("应该为不包含: actual={},expected={}, message={},params={}", actual, expected, message, params);
            throw new BusinessException(message);
        }
    } 
}
